<?php

namespace app\api\controller\xiluhouse;

use app\common\controller\Api;
use app\common\model\xiluhouse\Config;
use think\Db;

/**
 * @title   聊天类
 * @desc    获取聊天数据的接口
 */
class Chat extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @title  消息数据
     * @return int   $classify  1    消息类型:1=站内消息,2=每日推送,3=积分变动（跳转积分明细页面）,4=新房动态（跳转楼盘动态页面）
     * @param string $city   上海市  所在城市
     */
    public function msg_data()
    {
        // 站内消息
        $model = new \app\common\model\xiluhouse\Message;
        $user_ids = $this->auth->id ? $this->auth->id . ',0' : '0';
        $last_msg = $model->where('to_user_id', 'in', $user_ids)->whereIn('type', [1,2])->orderRaw('id desc')->find();
        $msg_count = $model->where('to_user_id', 'in', $user_ids)->where('read_status',1)->whereIn('type', [1,2])->count();
        $data[] = [
            'classify' => 1,
            'avatar' => getAvatar(Config::getdatas('msg_img1'), Config::getdatas('msg_name1')),
            'is_empty_avatar' => Config::getdatas('msg_img1') ? false : true,
            'name' => Config::getdatas('msg_name1'),
            'count' =>$msg_count,
            'msg' => $last_msg ? $last_msg['title'] : '暂无消息',
            'createtime_text' => $last_msg ? $last_msg['createtime_text'] : '',
        ];
        //签约提醒
        $last_msg = $model->where('to_user_id', 'in', $user_ids)->where('type', 4)->orderRaw('id desc')->find();
        $msg_count = $model->where('to_user_id', 'in', $user_ids)->where('read_status',1)->where('type', 4)->count();
        $data[] = [
            'classify' => 4,
            'avatar' => getAvatar(Config::getdatas('msg_img2'), Config::getdatas('msg_name2')),
            'is_empty_avatar' => Config::getdatas('msg_img2') ? false : true,
            'name' => Config::getdatas('msg_name2'),
            'count' =>$msg_count,
            'msg' => $last_msg ? $last_msg['title'] : '暂无消息',
            'createtime_text' => $last_msg ? $last_msg['createtime_text'] : '',
        ];
        // 新房动态
        $city = $this->request->param('city', '上海市');
        $house_trends_model = new \app\common\model\xiluhouse\HouseTrends;
        $last_msg = $house_trends_model->with(['house'])->where('house.city', 'like', $city . '%')->orderRaw('house_trends.id desc')->find();
        $msg_count = $house_trends_model->with(['house'])->where('house.city', 'like', $city . '%')->orderRaw('house_trends.id desc')->count();
        $data[] = [
            'classify' => 2,
            'avatar' => getAvatar(Config::getdatas('msg_img3'), Config::getdatas('msg_name3')),
            'is_empty_avatar' => Config::getdatas('msg_img3') ? false : true,
            'name' => Config::getdatas('msg_name3'),
            'count' =>$msg_count,
            'msg' => $last_msg ? $last_msg['name'] : '暂无消息',
            'createtime_text' => $last_msg ? $last_msg['createtime_text'] : '',
        ];

        // 每日推送
        $last_msg = $model->where('to_user_id', 'in', $user_ids)->where('type', 'eq', 3)->orderRaw('id desc')->find();
        $msg_count = $model->where('to_user_id', 'in', $user_ids)->where('read_status',1)->where('type', 3)->count();
        $data[] = [
            'classify' => 3,
            'avatar' => getAvatar(Config::getdatas('msg_img4'), Config::getdatas('msg_name4')),
            'is_empty_avatar' => Config::getdatas('msg_img4') ? false : true,
            'name' => Config::getdatas('msg_name4'),
            'count' =>$msg_count,
            'msg' => $last_msg ? $last_msg['title'] : '暂无消息',
            'createtime_text' => $last_msg ? $last_msg['createtime_text'] : '',
        ];
        //积分变动
//        $score_log_model = new \app\common\model\xiluhouse\ScoreLog;
//        $last_msg = $score_log_model->where('user_id', 'in', $user_ids)->orderRaw('id desc')->find();
//        $data[] = [
//            'classify' => 3,
//            'avatar' => getAvatar(Config::getdatas('msg_img3'), Config::getdatas('msg_name3')),
//            'is_empty_avatar' => Config::getdatas('msg_img3') ? false : true,
//            'name' => Config::getdatas('msg_name3'),
//            'msg' => $last_msg ? '你有新的积分动态' : '暂无消息',
//            'createtime_text' => $last_msg ? $last_msg['createtime_text'] : '',
//        ];

        $this->success('', $data);
    }

    /**
     * @title  系统消息
     *
     * @param int $classify  1   消息类型：1=站内消息,2=楼盘每日推送
     */
    public function message_list()
    {
        $classify = $this->request->param('classify/d', 1);
        $model = new \app\common\model\xiluhouse\Message;
        $user_ids = $this->auth->id ? $this->auth->id . ',0' : '0';
        if ($classify == 1) {
            $list = $model->where('to_user_id', 'in', $user_ids)->whereIn('type', [1,2])->orderRaw('id desc')->paginate();
        } else if($classify == 4){
            $list = $model->where('to_user_id', 'in', $user_ids)->whereIn('type', 4)->orderRaw('id desc')->paginate();
        }else{
            $list = $model->where('to_user_id', 'in', $user_ids)->where('type', 'eq', 3)->orderRaw('id desc')->paginate();
            foreach ($list as $key => &$value) {
                $value->append(['houseData']);
            }
        }

        $this->success('', $list);
    }

    /**
     * @title  系统消息详情
     *
     * @param int $id   0   消息id
     */
    public function message_detail()
    {
        $id = $this->request->param('id/d', 0);
        if (!$id) {
            $this->error('参数错误');
        }
        $model = new \app\common\model\xiluhouse\Message;
        $info = $model->find($id);
        if($info){
            $info->read_status = 0;
            $info->allowField(true)->save();
        }
        $this->success('', $info);
    }
}
